package cn.kgc.yigou.controller;

import cn.kgc.yigou.common.ResultEntity;
import cn.kgc.yigou.pojo.Shop;
import cn.kgc.yigou.service.ShopService;
import com.aliyun.oss.OSS;
import com.aliyun.oss.OSSClientBuilder;
import com.github.pagehelper.PageInfo;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.ResponseBody;

import javax.annotation.Resource;
import java.io.FileInputStream;
import java.io.FileNotFoundException;
import java.io.InputStream;

@Controller
public class ShopController {
    @Resource
    ShopService shopService;

    @RequestMapping("/yigou/shop/search/by/keyword")
    @ResponseBody
    public ResultEntity<PageInfo<Shop>> shop(@RequestParam(value = "pageNum", defaultValue = "1", required = false) Integer pageNum,
                                             @RequestParam(value = "keyword", defaultValue = "", required = false) String keyword){
        PageInfo<Shop> pageInfo = shopService.queryForKeywordWithPage(pageNum, keyword);
        return ResultEntity.successWithoutData(pageInfo);
    }
    @RequestMapping("/yigou/shop/shangchuan")
    @ResponseBody
    public void contextLoads() throws FileNotFoundException {
// Endpoint以杭州为例，其它Region请按实际情况填写。
        String endpoint = "http://oss-cn-beijing.aliyuncs.com";
        // 云账号AccessKey有所有API访问权限，建议遵循阿里云安全最佳实践，创建并使用RAM子账号进行API访问或日常运维，请登录 https://ram.console.aliyun.com 创建。
        String accessKeyId = "LTAI4FxKUrkEk9aWgWxrYTuc";
        String accessKeySecret = "9bA12BMT6RHHwExOkhDf87FfDRp1PQ";

        // 创建OSSClient实例。
        OSS ossClient = new OSSClientBuilder().build(endpoint, accessKeyId, accessKeySecret);

        // 声明要上传的文件名
        String fileName = "商品图片.jpg";

        // 声明上传文件后存入的目录的名称
        String folderName = "test";

        // 声明Bucket名称
        String bucketName = "xiaobaimm";

        // 存入对象的名称=目录名称+"/"+文件名
        String objectName = folderName + "/" + fileName;

        // 上传文件流。
        InputStream inputStream = new FileInputStream("D:\\图片\\"+fileName);
        ossClient.putObject(bucketName, objectName, inputStream);

        // 关闭OSSClient。
        ossClient.shutdown();
    }
}
